# coding: utf-8
from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from qfluentwidgets import SingleDirectionScrollArea, SegmentedWidget, TransparentToolButton, FluentIcon

from components import SlidingStackedWidget
from .courseware_widget import CoursewareWidget


class TreeInterface(QWidget):
    returnSignal = Signal()
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.horizontalLayout = QHBoxLayout()
        self.returnBtn = TransparentToolButton(FluentIcon.RETURN, self)
        self.pivot = SegmentedWidget(self)
        self.scrollArea = SingleDirectionScrollArea(self)
        self.stackedWidget = SlidingStackedWidget(self.scrollArea)
        self.coursewareWidget = CoursewareWidget(self.stackedWidget)

        self.__initWidgets()

    def __initWidgets(self):
        self.scrollArea.setWidget(self.stackedWidget)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)

        self.verticalLayout.setContentsMargins(11, 45, 11, 11)

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.addWidget(self.returnBtn)
        self.horizontalLayout.addWidget(self.pivot)

        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout.addWidget(self.scrollArea)

        self.pivot.addItem("courseware", "课程课件")
        self.stackedWidget.addWidget(self.coursewareWidget)

        self.pivot.setCurrentItem('courseware')

        self.pivot.currentItemChanged.connect(self.onPivotItemChanged)
        self.returnBtn.clicked.connect(self.returnSignal)

    def onPivotItemChanged(self, routeKey):
        mapping = {
            'courseware': self.coursewareWidget
        }
        self.stackedWidget.setCurrentWidget(mapping[routeKey])
